package com.qf.web.servlet;

import com.qf.pojo.Manager;
import com.qf.service.ManagerService;
import com.qf.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * 登录功能： 判断数据库中是否存在 用户名和密码
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        //获取（session， 用户输入的）验证码
        HttpSession session = req.getSession();
        //session验证码
        Object code = session.getAttribute("code");
        //用户输入的验证码
        String inputCode = req.getParameter("code");
        if(code == null) {
            //登录失败
            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>登录状态页面</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("验证码失效");
            writer.println("</body>");
            writer.println("</html>");
            return;
        }else {
            String str = (String) code;
            if(!str.equalsIgnoreCase(inputCode)) {
                //登录失败
                writer.println("<html>");
                writer.println("<head>");
                writer.println("<title>登录状态页面</title>");
                writer.println("</head>");
                writer.println("<body>");
                writer.println("验证码输入错误");
                writer.println("</body>");

                writer.println("</html>");
                return;
            }
        }



        //获取请求数据
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");

        //以 username, pwd 为条件， 在数据库查询管理员对象，
        // 如果查询到，则认为登录成功，
        // 否则认为登录失败

        ManagerService ms = new ManagerServiceImpl();

        Manager manager = ms.select(username, pwd);

        if(manager == null) {
            //登录失败
            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>登录状态页面</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("登录失败");
            writer.println("</body>");

            writer.println("</html>");
        }else {
            //登录成功
//            writer.println("<html>");
//            writer.println("<head>");
//            writer.println("<title>登录状态页面</title>");
//            writer.println("</head>");
//            writer.println("<body>");
//            writer.println("登录成功");
//            writer.println("</body>");
//            writer.println("</html>");

//          如果登录成功， 跳转路径， 查询所有的员工

            //通过session保存登录状态
            session.setAttribute("manager", manager);

            //selectAllEmp
            resp.sendRedirect("/selectAllEmp");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
